package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.HouseHoldinfos;
import com.woniu.entity.Households;
import com.woniu.mapper.HouseHoldinfosMapper;
import com.woniu.mapper.HouseholdsMapper;
import com.woniu.service.IHouseHoldinfosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
@Service
public class HouseHoldinfosServiceImpl extends ServiceImpl<HouseHoldinfosMapper, HouseHoldinfos> implements IHouseHoldinfosService {

    @Autowired
    private HouseHoldinfosMapper houseHoldinfosMapper;

    @Autowired
    private HouseholdsMapper householdsMapper;

    /**
     查询全部
     */
    @Override
    public IPage<HouseHoldinfos> findAll(HouseHoldinfos houseHoldinfos) {
        Page<HouseHoldinfos> page=new Page<>(houseHoldinfos.getCurrent(),houseHoldinfos.getPageSize());
        QueryWrapper<HouseHoldinfos> queryWrapper = new QueryWrapper<>();
        IPage<HouseHoldinfos> pageData  = houseHoldinfosMapper.customPageQuery(page,queryWrapper);
        return pageData;
    }

    /**
     查询一个
     */

    @Override
    public HouseHoldinfos findById(String hid) {
        HouseHoldinfos houseHoldinfos = houseHoldinfosMapper.selectById(hid);
        return houseHoldinfos;
    }

    //想给房屋表增加，再增加详情表
    @Transactional
    @Override
    public int CustomSave(HouseHoldinfos houseHoldinfos) {
        Households households=new Households();
        households.setFloorId(houseHoldinfos.getFloorId());
        households.setHouseholdName(houseHoldinfos.getHouseType());
        int insert = householdsMapper.insert(households);
        if(insert > 0){
            //新增成功之后在查到该id
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.orderByDesc("HouseHold_ID");
            //降序排序，取第一个
            List<Households> list = householdsMapper.selectList(queryWrapper);
            HouseHoldinfos f=new HouseHoldinfos();
            f.setHouseholdId(list.get(0).getHouseholdId());
            f.setHouseType(houseHoldinfos.getHouseType());
            f.setToward(houseHoldinfos.getToward());
            f.setRenovationSituation(houseHoldinfos.getRenovationSituation());
            f.setConstructionArea(houseHoldinfos.getConstructionArea());
            f.setAccurateArea(houseHoldinfos.getAccurateArea());
            f.setPrice(houseHoldinfos.getPrice());
            f.setPropertyCosts(houseHoldinfos.getPropertyCosts());
            f.setState(1);
            insert = houseHoldinfosMapper.insert(f);
        }

        return insert;
    }

    /**
     模糊查询
     */
    @Override
    public List<HouseHoldinfos> likeFind(HouseHoldinfos houseHoldinfos) {
        QueryWrapper queryWrapper=new QueryWrapper();
        Map<String,String> map=new HashMap<>();
        if(houseHoldinfos.getBuildingName()!=""){
            map.put("Building_Name",houseHoldinfos.getBuildingName());
        }
        if(houseHoldinfos.getUnitName()!=""){
            map.put("Unit_Name",houseHoldinfos.getUnitName());
        }
        if(houseHoldinfos.getFloorName()!=""){
            map.put("Floor_Name",houseHoldinfos.getFloorName());
        }
        queryWrapper.allEq(map);
        List<HouseHoldinfos> list = houseHoldinfosMapper.find(queryWrapper);
        return list;
    }

    /**
     梁磊查询一个
     */
    @Override
    public List<HouseHoldinfos> findByIds(String hid) {
        QueryWrapper<HouseHoldinfos> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("HouseHoldInfo_ID",hid);
        List<HouseHoldinfos> houseHoldinfos = houseHoldinfosMapper.find(queryWrapper);
        return houseHoldinfos;
    }

}
